home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1697 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  50 lines

  1. Newsgroups: comp.lang.c
  2. Path: sunsite.doc.ic.ac.uk!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: is this string a number?
  5. Message-ID: <DL9tzD.H0@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <4dbogk$763@jupiter.planet.net> <30f9ac87.195826304@nntp.ix.netcom.com>
  11. Date: Tue, 16 Jan 1996 11:16:25 GMT
  12.  
  13. Mike Rubenstein (miker3@ix.netcom.com) wrote:
  14. : Chris Kemp <chrisk@paladn.com> wrote:
  15.  
  16. : |>I am inputting a string from the keyboard from a user, and 
  17. : |>intend to use the atol or strtol functions to convert the 
  18. : |>string to a number.
  19.  
  20. : Use strtol() and supply a non-null second argument.  When strtol()
  21. : returns, the pointer will point to the first character that was not
  22. : converted.  Example:
  23.  
  24. :     #include <stdlib.h>
  25.  
  26. :     char *str;
  27. :     char *nstr;
  28. :     long l;
  29.  
  30. :     /* ... */
  31.  
  32. :     l = strtol(str, &nstr, 10);
  33. :     if (nstr == str || *nstr !=  '\0')
  34. :     {
  35. :       /* str was empty or there is an illegal character */
  36. :     }
  37.  
  38. strtol et al remove leading white space, thus if the string " " is supplied
  39. the above check won't tell if the string was blank (nstr and str will
  40. be different and *nstr will be 0). Depending on
  41. your application this may or may not be a problem.
  42.  
  43. nathan
  44. --
  45. Nathan Sidwell                         Holder of the Xmris home page
  46. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  47. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  48. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  49. Having problems?     try http://www.pact.srf.ac.uk/~nathan/problems/
  50.